package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.Date;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/2/15 19:05
 */
public class KnowledgeBaseQueryFragment extends Fragment {
  protected static final String EXTRA_CONTENT =
      "com.isunland.managesystem.ui.KnowledgeBaseQueryFragment.EXTRA_CONTENT";

  private final int DATA_STATUS = 0;
  private final int BEGINREG_DATE = 1;
  private final int ENDREG_DATE = 2;
  private final int DOC_KIND = 3;
  @BindView(R.id.tv_titleRight) TextView tvDataStatus;
  @BindView(R.id.ib_dataStatus) ImageView ibDataStatus;
  @BindView(R.id.tv_beginregDate) TextView tvBeginregDate;
  @BindView(R.id.ib_beginregDate) ImageView ibBeginregDate;
  @BindView(R.id.tv_endregDate) TextView tvEndregDate;
  @BindView(R.id.ib_endregDate) ImageView ibEndregDate;
  @BindView(R.id.tv_docKind) TextView tvDocKind;
  @BindView(R.id.ib_docKind) ImageView ibDocKind;
  @BindView(R.id.et_docKeyWords) EditText etDocKeyWords;

  private String dataStatusCode;
  private String dataStatusName;
  private String beginregDate;
  private String endregDate;
  private String docKindCode;
  private String docKindName;
  private String docKeyWords;

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    BaseVolleyActivity activity = (BaseVolleyActivity) getActivity();
    if (activity != null) {
      ActionBar actionBar = activity.getSupportActionBar();
      if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle(R.string.query);
      }
    }
    ExtraValueContent queryConten =
        (ExtraValueContent) getArguments().getSerializable(EXTRA_CONTENT);
    if (queryConten != null) {
      dataStatusName = queryConten.getExtName1();
      dataStatusCode = queryConten.getExtCode1();
      beginregDate = queryConten.getExtName2();
      endregDate = queryConten.getExtName3();
      docKindName = queryConten.getExtName4();
      docKindCode = queryConten.getExtCode4();
      docKeyWords = queryConten.getExtName5();
    }
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        //                if (NavUtils.getParentActivityName(getActivity()) != null) {
        //                    NavUtils.navigateUpFromSameTask(getActivity());
        //                }
        getActivity().finish();
        return true;
      case R.id.menu_item_confirm:
        ExtraValueContent queryConten = new ExtraValueContent();
        queryConten.setExtName1(dataStatusName);
        queryConten.setExtCode1(dataStatusCode);
        queryConten.setExtName2(beginregDate);
        queryConten.setExtName3(endregDate);
        queryConten.setExtName4(docKindName);
        queryConten.setExtCode4(docKindCode);
        queryConten.setExtName5(etDocKeyWords.getText().toString());
        Intent intent = new Intent();
        intent.putExtra(EXTRA_CONTENT, queryConten);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_knowledge_query, container, false);
    ButterKnife.bind(this, view);
    initData();
    return view;
  }

  private void initData() {
    tvDataStatus.setText(dataStatusName);
    tvBeginregDate.setText(beginregDate);
    tvEndregDate.setText(endregDate);
    tvDocKind.setText(docKindName);
    etDocKeyWords.setText(docKeyWords);
  }

  @OnClick({ R.id.ib_dataStatus, R.id.ib_beginregDate, R.id.ib_endregDate, R.id.ib_docKind })
  public void onClick(View view) {
    switch (view.getId()) {
      case R.id.ib_dataStatus:
        showDialog(DATA_STATUS);
        break;
      case R.id.ib_beginregDate:
        showDialog(BEGINREG_DATE);
        break;
      case R.id.ib_endregDate:
        showDialog(ENDREG_DATE);
        break;
      case R.id.ib_docKind:
        showDialog(DOC_KIND);
        break;
    }
  }

  private void showDialog(int requestCode) {
    DialogFragment dialog = null;
    switch (requestCode) {
      case DATA_STATUS:
        dialog = new KnowlegeDataStatusDialogFragment();
        break;
      case BEGINREG_DATE:
        dialog = new BaseYMDTimeDialogFragment();
        break;
      case ENDREG_DATE:
        dialog = new BaseYMDTimeDialogFragment();
        break;
      case DOC_KIND:
        dialog = PublishKnowledgeTypeDialogFragment.newInstance(
            PublishKnowledgeTypeDialogFragment.KNOWLEDGE_BASE);
        break;
    }
    FragmentManager fm = getFragmentManager();
    dialog.setTargetFragment(this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == DATA_STATUS) {
      CustomerDialog content = (CustomerDialog) data.getSerializableExtra(
          RequisitionDataStatusDialogFragment.EXTRA_DATA_STATUS);
      dataStatusCode = content.getId();
      dataStatusName = content.getName();
      tvDataStatus.setText(dataStatusName);
    }
    if (requestCode == BEGINREG_DATE) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      beginregDate = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      tvBeginregDate.setText(beginregDate);
    }
    if (requestCode == ENDREG_DATE) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      endregDate = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      tvEndregDate.setText(endregDate);
    }
    if (requestCode == DOC_KIND) {
      CustomerDialog dialog =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      docKindName = dialog.getName();
      docKindCode = dialog.getId();
      tvDocKind.setText(docKindName);
    }
  }

  public static KnowledgeBaseQueryFragment newInstance(ExtraValueContent queryConten) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, queryConten);
    KnowledgeBaseQueryFragment fragment = new KnowledgeBaseQueryFragment();
    fragment.setArguments(args);
    return fragment;
  }
}